package top.lixunda.ecommerce.server.good.api.entity.vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.beans.BeanUtils;
import top.lixunda.common.utils.ObjectValidators;
import top.lixunda.ecommerce.server.good.api.entity.db.GoodsDO;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Xumda
 * @version time:2019/12/27 16:42
 * <p>
 * 用于上前端传递数据渲染页面,实际上是对GoodsDO中的某些数据进行影藏，如逻辑删除字段
 */
@SuppressWarnings("WeakerAccess")
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
public class GoodsVO {

    /**
     * 商品唯一标识Id
     */
    private Integer id;

    /**
     * 商品分组id
     */
    private Integer groupId;

    /**
     * 分组名称
     */
    private String groupName;

    /**
     * 店铺id
     */
    private Integer storeId;

    /**
     * 店铺名称
     */
    private String storeName;

    /**
     * 商品名称
     */
    private String name;

    /**
     * 商品简要描述，一句话
     */
    private String simpleDesc;

    /**
     * 商品详细描述
     */
    private String description;

    /**
     * 分类id，系统分类
     */
    private Integer goodCategoryId;

    /**
     * 分类id，系统分类
     */
    private String goodCategoryName;

    /**
     * 买点
     */
    private String sellPoint;

    /**
     * 热度得分
     */
    private BigDecimal score;

    /**
     * 热度得分上次更新时间，每天更新一次
     */
    private LocalDateTime scoreUpdateTime;

    /**
     * 运费，为0时表示包邮
     */
    private BigDecimal freight;

    /**
     * 上架时间
     */
    private LocalDateTime pushTime;

    /**
     * 下架时间
     */
    private LocalDateTime pullTime;

    /**
     * 创建时间
     */
    private LocalDateTime createTime;

    /**
     * 状态
     */
    private String state;

    /**
     * 备注信息
     */
    private String remark;

    public static GoodsVO fromGoodsDO(GoodsDO goodsDO) {
        if (goodsDO == null) {
            return null;
        }
        GoodsVO vo = new GoodsVO();
        BeanUtils.copyProperties(goodsDO, vo);
        return vo;
    }

    public static List<GoodsVO> fromGoodsDO(List<GoodsDO> goodsDoList) {
        if (goodsDoList == null) {
            return null;
        }
        if (ObjectValidators.isEmpty(goodsDoList)) {
            return Collections.emptyList();
        }
        List<GoodsVO> list = new ArrayList<>(goodsDoList.size());
        GoodsVO v;
        for (GoodsDO goodsDO : goodsDoList) {
            v = fromGoodsDO(goodsDO);
            if (v != null) {
                list.add(v);
            }
        }
        return list;
    }

}
